package com.mtsmda.examples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Pangram {
	private static final String NOT_PANGRAM = "not pangram";
	private static final String PANGRAM = "pangram";

	public static void main(String[] args) {
		String sentence = "The quick brown fox jumps over the lazy dog";
		List<Character> setAllAlfabet = setAllAlfabet();
		System.out
				.println(checkIsPangram(sentence.toLowerCase(), setAllAlfabet));
		System.out.println(checkIsPangramV2(sentence.toLowerCase(),
				fillMap(setAllAlfabet)));
	}

	private static List<Character> setAllAlfabet() {
		List<Character> characters = new ArrayList<Character>(26);
		for (int i = 97; i < 123; i++) {
			characters.add((char) i);
		}
		return characters;
	}

	private static Map<Character, Boolean> fillMap(List<Character> characters) {
		Map<Character, Boolean> alfabet = new HashMap<>(26);
		for (Character character : characters) {
			alfabet.put(character, false);
		}
		return alfabet;
	}

	private static String checkIsPangram(String sentence,
			List<Character> setAllAlfabet) {
		long begin = System.nanoTime();
		System.out.println("begin");
		if (sentence != null && !sentence.isEmpty()) {
			for (int i = 0; i < sentence.length(); i++) {
				if (setAllAlfabet.contains(sentence.charAt(i))) {
					char charAt = sentence.charAt(i);
					setAllAlfabet.remove(setAllAlfabet.indexOf(charAt));
				}
			}
			if (setAllAlfabet.isEmpty()) {
				System.out.println("end");
				long end = System.nanoTime();
				System.out.println(end - begin);
				return PANGRAM;
			}
		}
		System.out.println("end");
		long end = System.nanoTime();
		System.out.println(end - begin);
		return NOT_PANGRAM;
	}

	private static String checkIsPangramV2(String sentence,
			Map<Character, Boolean> alfabet) {
		long begin = System.nanoTime();
		System.out.println("begin");
		if (sentence != null && !sentence.isEmpty()) {
			for (int i = 0; i < sentence.length(); i++) {
				alfabet.put(sentence.charAt(i), true);
			}
			if (!alfabet.containsValue(false)) {
				System.out.println("end");
				long end = System.nanoTime();
				System.out.println(end - begin);
				return PANGRAM;
			}
		}
		System.out.println("end");
		long end = System.nanoTime();
		System.out.println(end - begin);
		return NOT_PANGRAM;
	}
}